﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

using NUnit.Framework;

using Engine;
using Engine.Components.CompArch;

namespace Engine.Tests
{
    /// <summary>
    /// A CompositeComponent consisting of a RandomMemoryAccessorCpu connected to a MemoryBank.  Stress test to
    /// see if everything plays nicely in the sandbox.
    /// </summary>
    [TestFixture]
    public class MemoryTester: CompositeComponent
    {
        RandomMemoryAccessorCpu cpu = new RandomMemoryAccessorCpu();
        MemoryBank mem = new MemoryBank();

        [Test]
        public void CpuDrivingMemoryWithRandomizedReadsAndWrites()
        {
            mem.Alignment.Value = AlignmentBoundary.WordAddressable;
            base.Connect<UInt32>(cpu.MemoryAddressRegister, mem.Address);
            base.Connect<byte[]>(cpu.MemoryWriteRegister, mem.WritePort);
            base.Connect<bool>(cpu.RWFlag, mem.IsWrite);
            base.Connect<byte[]>(mem.ReadPort, cpu.MemoryReadRegister);
            base.Connect<bool>(mem.InvalidAddressFlag, cpu.MemErrorFlag);
            cpu.Start();
            Assert.IsTrue(cpu.SuccessfulReads != 0);
            Assert.IsTrue(cpu.SuccessfulWrites != 0);
            Assert.IsTrue(cpu.SuccessfulWrites == mem.Writes);
            Assert.IsTrue(cpu.SuccessfulReads == mem.Reads);
        }
    }
}
